/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import com.controller.BeberageManager;
import com.gui.BeberageManagementForm;
import com.gui.CreateBeberageForm;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Miguel
 */
public class CreateBeberageView implements ActionListener {

    public CreateBeberageView() {
        initializeCreateBeberageForm();
        initializeButtons();
        initializeTextFields();

        beberageManager = BeberageManager.getInstance();
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String actionCommand = actionEvent.getActionCommand();
        if (actionCommand.equalsIgnoreCase(CREATE_BEBERAGE)) {
            //Call BeberageManager to create Beberage
            String beberageName = beberageNameTextField.getText();
            String beberageDescription = beberageDescriptionTextArea.getText();
            beberageManager.registerBeberage(beberageName, beberageDescription, this);
        } else if (actionCommand.equalsIgnoreCase(CANCEL_CREATION)) {
            //Call BeberageManager to cancelCreation
            closeBeberageCreationForm();
        }
    }

    public void showBeberageCreationForm(){
        createBeberageForm.setVisible(true);
    }
    
    public void closeBeberageCreationForm(){
        createBeberageForm.dispose();
    }
    
    public void showMessage(String msg){
        JOptionPane.showMessageDialog(createBeberageForm,msg,"Mensaje",JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void initializeCreateBeberageForm(){
        createBeberageForm = new CreateBeberageForm();
        createBeberageForm.setTitle("Registrar Bebidas");
        createBeberageForm.setModalityType(ModalityType.TOOLKIT_MODAL);
    }
    
    private void initializeButtons() {
        
        createBeberageButton = createBeberageForm.getCreateBeberageButton();
        createBeberageButton.setActionCommand(CREATE_BEBERAGE);
        createBeberageButton.addActionListener(this);

        cancelCreateButton = createBeberageForm.getCancelCreationButton();
        cancelCreateButton.setActionCommand(CANCEL_CREATION);
        cancelCreateButton.addActionListener(this);
    }

    private void initializeTextFields() {
        
        beberageNameTextField = createBeberageForm.getBeberageNameTextField();
        beberageDescriptionTextArea = createBeberageForm.getBeberageDescriptionTextArea();
    }
    
    private JButton createBeberageButton;
    private JButton cancelCreateButton;
    private JTextField beberageNameTextField;
    private JTextArea beberageDescriptionTextArea;
    private BeberageManager beberageManager;
    private CreateBeberageForm createBeberageForm;
    private BeberageManagementForm parentForm;
    
    private final String CREATE_BEBERAGE = "CREATE_BEBERAGE";
    private final String CANCEL_CREATION = "CANCEL_CREATION";
}
